package naito_rescue.util.comm.team;

import java.util.*;

import sample.*;
import rescuecore2.messages.*;
import rescuecore2.worldmodel.*;
import rescuecore2.Constants;
import rescuecore2.config.*;
import rescuecore2.standard.components.*;
import rescuecore2.standard.entities.*;
import rescuecore2.standard.messages.*;
import rescuecore2.misc.geometry.*;

import naito_rescue.*;
import naito_rescue.agent.*;
import naito_rescue.object.*;
import naito_rescue.util.sort.*;
import naito_rescue.communication.team.*;
import naito_rescue.communication.channel.*;
import naito_rescue.message.*;
import naito_rescue.message.manager.*;

public class CommunicationTeamUtil
{
	// FB1, PF1, AT1, FB2, PF2, AT2, ... , FBn, PFn, ATnなリストを作って返す
	// @param FB, PF, ATの混在したStandardEntityのリスト
	public static List<StandardEntity> 
	createAgentCluster(List<StandardEntity> allPlatoons){
		List<StandardEntity> result = new ArrayList<StandardEntity>();
		List<StandardEntity> allFB = new ArrayList<StandardEntity>();
		List<StandardEntity> allPF = new ArrayList<StandardEntity>();
		List<StandardEntity> allAT = new ArrayList<StandardEntity>();
		
		for(StandardEntity entity : allPlatoons){
			if(entity instanceof FireBrigade)
				allFB.add(entity);
			else if(entity instanceof PoliceForce)
				allPF.add(entity);
			else if(entity instanceof AmbulanceTeam)
				allAT.add(entity);
		}
		Collections.sort(allFB, ComparatorUtils.ID_Ascending_Comparator);
		Collections.sort(allPF, ComparatorUtils.ID_Ascending_Comparator);
		Collections.sort(allAT, ComparatorUtils.ID_Ascending_Comparator);
		
		while(!allFB.isEmpty() || !allPF.isEmpty() || !allAT.isEmpty()){
			if(!allFB.isEmpty()) result.add(allFB.remove(0));
			if(!allPF.isEmpty()) result.add(allPF.remove(0));
			if(!allAT.isEmpty()) result.add(allAT.remove(0));
		}
		return result;
	}
	
	public static List<AbstractChannel>
	getChannelsToOtherTeam(List<? extends AbstractChannel> channels, AbstractChannel myTeamChannel){
		List<AbstractChannel> result = new ArrayList<AbstractChannel>();
		
		int num = myTeamChannel.getChannelNum();
		for(AbstractChannel ch : channels){
			if(num != ch.getChannelNum()){
				result.add(ch);
			}
		}
		return result;
	}
	public static List<StandardEntity>
	getAgentsAfterRotation(List<StandardEntity> members,
	                       int timestep,
	                       int ignoreUntil,
	                       int numOfRotationMember){
		List<StandardEntity> subMembers = new ArrayList<StandardEntity>();
		subMembers.addAll(members);
		
		int repeat = timestep - ignoreUntil;
		while(repeat-- > 0){
			Collections.rotate(subMembers, numOfRotationMember);
		}
		return subMembers.subList(0, numOfRotationMember);
	}
}
